package stream;

import com.ibm.tspaces.TupleSpace;
import com.ibm.tspaces.TupleSpaceException;

public class Test {
  public static void main(String[] args) 
  throws TupleSpaceException, InterruptedException {
    int             rank  = Integer.parseInt(args[0]);
    int             elems = Integer.parseInt(args[1]);
    TupleSpace      ts    = new TupleSpace("strmSpace", "localhost");
    Stream<Integer> strm  = new Stream<Integer>("strm", Integer.class, ts);
    
    if (rank == 0) {
      ts.deleteAll();
      strm.init();
    }
    
    Thread.sleep(2000);
    
    if (rank == 0) {
      for (int i = 0; i < elems; i++) {
        System.out.printf("Client %d: append %d.\n", rank, i);
        strm.append(i);
      }
    } else {
      for (int i = 0; i < elems; i++) {
        int elem = strm.remove();
        System.out.printf("Client %d: remove %d.\n", rank, elem);
      }
    }
    
    System.err.printf("Client %d finished.\n", rank);
  }
}
